use crate::component::request::Request;
use crate::component::response::Response;
use crate::component::ParseResult;
use url::Url;
use log::{debug, error};


pub trait Spider: Send + Sync {
    type ItemType: Clone;

    /// 需要 Spider 有个  start_urls: Vec<&str> 字段
    fn start_urls(&self) -> &[&'static str];

    /// 构建入口请求
    /// 默认 `GET` 方式发送 `self.start_urls` 中的 urls,
    /// 默认 `self.parse()` 作为解析函数(即什么都不做)
    fn start_requests(&self) -> Vec<Request<Self::ItemType>> {
        let mut reqs = vec![];
        for url in self.start_urls() {
            match Url::parse(url) {
                Ok(url) => {
                    debug!("为 [{}] 创建请求", url);
                    let req = Request::simple_get(url, None, None);
                    reqs.push(req);
                }
                Err(e) => error!("创建请求失败, {}",e),
            }
        }
        reqs
    }

    /// 默认解析函数
    fn parse(&self, _resp: Response<Self::ItemType>) -> ParseResult<Self::ItemType> {
        // self.log("默认解析函数, 什么都不做");
        ParseResult::new(vec![], vec![])
    }
}
